<?php

namespace app\admin\controller;

use think\Db;
use think\Session;

class User extends Authorized
{

    public function index(){return $this->fetch();}
    public function agent(){return $this->fetch();}
    public function stop(){return $this->fetch();}
    public function edit_xs(){

        $post_data = $this->request->post();
        if($post_data){
            $post_data['update_time']=date('Y-m-d H:i:s',time());
            Db::table('user')->startTrans();
            $id =Db::table('user')->update($post_data);
            if (false === $id) {
                Db::table('user')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'分配失败','data'=>'']);
            } else {
                Db::table('user')->commit();
                return json(['code' =>0,'count' =>0,'msg' =>'分配成功','data'=>'']);
            }
        }

        $id = input('id');
        $article=Db::table('user')->where('id',$id)->find();
        $this->assign('article', $article);

        $xs_list=Db::table('admin')->where(['state'=>1,'role_id'=>2])->order('username')->select();
        $this->assign('xs_list', $xs_list);
        return $this->fetch();
    }
    public function edit_agent(){

        $post_data = $this->request->post();
        if($post_data){
            $post_data['update_time']=date('Y-m-d H:i:s',time());
            Db::table('user')->startTrans();
            return json(['code' =>1,'count' =>0,'msg' =>$post_data,'data'=>'']);
            $id =Db::table('user')->update($post_data);

            if (false === $id) {
                Db::table('user')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'分配失败','data'=>'']);
            } else {
                Db::table('user')->commit();
                return json(['code' =>0,'count' =>0,'msg' =>'分配成功','data'=>'']);
            }
        }

        $id = input('id');
        $article=Db::table('user')->where('id',$id)->find();
        $this->assign('article', $article);


        $config_price=Db::table('config')->where('id',1)->find();
        $this->assign('config_price', $config_price);
        return $this->fetch();
    }


    public function getList()
    {
        $get_data=$this->request->get();
        $post_data = $this->request->post();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $orderType = input('order');  //排序
        $keywords = input('keywords');
        $usertype=input('usertype');
        $istop= input('istop');
        $where=[];
        $order='';
        

        if (!empty($orderType)){
//            if ($orderType=='money_asc'){$order='money';}
//            if ($orderType=='money_desc'){$order='money desc';}
//            if ($orderType=='moneypay_asc'){$order='moneypay';}
//            if ($orderType=='moneypay_desc'){$order='moneypay desc';}
//            if ($orderType=='closetime_asc'){$order='closetime asc';}
//            if ($orderType=='closetime_desc'){$order='closetime desc';}
//            if ($orderType=='serviceEndTime_asc'){$order='serviceEndTime asc';}
//            if ($orderType=='serviceEndTime_desc'){$order='serviceEndTime desc';}
        }else{
            $order='id desc';
        }

        if (!empty($usertype)){
            if ($usertype=='state'){   $where['u.state']=0;}
            if ($usertype=='zc'){   $where['u.state']=1;$where['u.is_agent']=0; } //正常客户 不包含代理商
            if ($usertype=='agent'){   $where['u.state']=1;$where['u.is_agent']=1; } //代理商
            if ($usertype=='stop'){
                $where['u.state']=2;

            }
            //丢单客户

        }



        if (!empty($istop)){
            if($istop==1){$where['isTop']=1;}
            if($istop==2){$where['isTop']=0;}

        }

        if (!empty($keywords)){
            $where['u.name']=['like','%'.$keywords.'%'];
        }


        $count= Db::table('user u')
                ->field('u.*,a.username xs_name')
                ->join('admin a','a.id=u.xs_id','left')
                ->where($where)
                ->count();
        $rows=Db::table('user u')
            ->field('u.*,a.state xs_state,a.username xs_name')
            ->join('admin a','a.id=u.xs_id','left')
            ->where($where)->order($order)->page($page, $limit)->select();

//        foreach ($rows as $k =>$v){
//            $num=Db::table('webinfo')->where(['state'=>1])->count('id');
//            if($num>0){
//                Db::table('user')->where('id',$v['id'])->update(['isTop'=>1]);
//            }else{
//                Db::table('user')->where('id',$v['id'])->update(['isTop'=>0]);
//            }
//        }
        return json(['code' =>0,'count' =>$count,'msg' =>Db::table('user')->getLastSql(),'data'=>$rows]);
    }

    public function add()
    {
        $post_data = $this->request->post();
        if($post_data){
            $is_agent=input('is_agent');
            if($is_agent){
                $post_data['is_agent']=$is_agent;
            }
            Db::table('user')->startTrans();
            $id =Db::table('user')->insertGetId($post_data);
            if (false === $id) {
                Db::table('user')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'添加失败','data'=>'']);
            } else {
               Db::table('user')->commit();
                return json(['code' =>0,'count' =>0,'msg' =>'添加成功','data'=>'']);
            }
        }

       return $this->fetch();
    }

    public function edit()
    {
        $post_data = $this->request->post();
        if($post_data){
            $post_data['update_time']=date('Y-m-d H:i:s',time());
            $result =Db::table('user')->update($post_data);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }


        $article=Db::table('user')->where('id' , input('id'))->find();
        $this->assign('article', $article);
        return $this->fetch();
    }


    public function del()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('user')->where('id', $post_data['id'])->delete();
            if (false === $result) {
                return json(['msg' =>'删除失败', 'code' =>1]);
            } else {
                return json(['msg' =>'删除成功', 'code' =>0]);
            }
        }
    }

    public function stop_user()
    {
        $post_data = $this->request->post();
        if($post_data){
            Db::table('user')->startTrans();

            $post_data['state']=2;
            $post_data['update_time']=$post_data['close_time']=date('Y-m-d H:i:s',time());
            $result =Db::table('user')->update($post_data);

            if (false === $result) {
                Db::table('user')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'锁定失败','data'=>'']);
            } else {
                Db::table('user')->commit();
                return json(['code' =>0,'count' =>0,'msg' =>'锁定成功','data'=>'']);
            }
        }
    }

    public function reup_user()
    {
        $post_data = $this->request->post();
        if($post_data){
            Db::table('user')->startTrans();
            $result =Db::table('user')->where('id', $post_data['id'])->setField(['state'=>1]);

            if (false === $result) {
                Db::table('user')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'启用失败','data'=>'']);
            } else {
                Db::table('user')->commit();
                return json(['code' =>0,'count' =>0,'msg' =>'启用成功','data'=>'']);
            }
        }
    }
}